import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class StudentGrade {
    private String name;
    private String id;
    private String course;
    private double score;

    public StudentGrade(String name, String id, String course, double score) {
        this.name = name;
        this.id = id;
        this.course = course;
        this.score = score;
    }

    // Getters
    public String getName() { return name; }
    public String getId() { return id; }
    public String getCourse() { return course; }
    public double getScore() { return score; }

    @Override
    public String toString() {
        return String.format("姓名：%s，学号：%s，课程：%s，成绩：%.1f", name, id, course, score);
    }
}

public class StudentGradeManagementSystem {
    private static List<StudentGrade> gradeRecords = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        showMainMenu();
    }

    private static void showMainMenu() {
        while (true) {
            System.out.println("\n欢迎使用学生成绩管理系统");
            System.out.println("请选择操作：");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计课程成绩");
            System.out.println("4. 退出系统");
            System.out.print("请输入选项序号：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    recordStudentGrade();
                    break;
                case 2:
                    queryStudentGrade();
                    break;
                case 3:
                    analyzeCourseGrade();
                    break;
                case 4:
                    System.out.println("\n感谢使用学生成绩管理系统。");
                    return;
                default:
                    System.out.println("无效选项，请重新输入。");
            }
        }
    }

    private static void recordStudentGrade() {
        System.out.println("\n【记录学生成绩】");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();

        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();

        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        System.out.print("请输入成绩（0-100）：");
        double score = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符

        if (score < 0 || score > 100) {
            System.out.println("成绩无效，请输入0-100之间的数值。");
            return;
        }

        gradeRecords.add(new StudentGrade(name, id, course, score));
        System.out.println("成绩已成功记录");
    }

    private static void queryStudentGrade() {
        System.out.println("\n【查询学生成绩】");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        System.out.print("请输入选项序号：");

        int choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        List<StudentGrade> results = new ArrayList<>();

        switch (choice) {
            case 1:
                System.out.print("请输入学生姓名：");
                String name = scanner.nextLine();
                for (StudentGrade record : gradeRecords) {
                    if (record.getName().equals(name)) {
                        results.add(record);
                    }
                }
                break;
            case 2:
                System.out.print("请输入学生学号：");
                String id = scanner.nextLine();
                for (StudentGrade record : gradeRecords) {
                    if (record.getId().equals(id)) {
                        results.add(record);
                    }
                }
                break;
            case 3:
                System.out.print("请输入课程名称：");
                String course = scanner.nextLine();
                for (StudentGrade record : gradeRecords) {
                    if (record.getCourse().equals(course)) {
                        results.add(record);
                    }
                }
                break;
            default:
                System.out.println("无效选项，返回主菜单。");
                return;
        }

        if (results.isEmpty()) {
            System.out.println("未找到匹配的成绩记录。");
        } else {
            System.out.println("查询结果：");
            for (StudentGrade record : results) {
                System.out.println(record);
            }
        }
    }

    private static void analyzeCourseGrade() {
        System.out.println("\n【统计课程成绩】");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        List<Double> scores = new ArrayList<>();
        for (StudentGrade record : gradeRecords) {
            if (record.getCourse().equals(course)) {
                scores.add(record.getScore());
            }
        }

        if (scores.isEmpty()) {
            System.out.println("未找到该课程的成绩记录。");
            return;
        }

        double sum = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;

        for (double score : scores) {
            sum += score;
            if (score > max) max = score;
            if (score < min) min = score;
        }

        double average = sum / scores.size();

        System.out.println("课程：" + course);
        System.out.printf("平均分：%.2f\n", average);
        System.out.printf("最高分：%.1f\n", max);
        System.out.printf("最低分：%.1f\n", min);
    }
}